<template>
<div>
<template>

<!-- 添加宿舍长隐藏端口 -->
<el-dialog title="添加宿舍长" :visible.sync="dialogFormVisible">
  <el-form :model="dormitoryHead">
    <el-form-item label="姓名" label-width="120px">
      <el-input v-model="dormitoryHead.username" autocomplete="off"></el-input>
    </el-form-item> 
    <el-form-item label="密码" label-width="120px">
      <el-input type="password" v-model="dormitoryHead.password" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" label-width="120px">
      <el-input v-model="dormitoryHead.email" autocomplete="off"></el-input>
    </el-form-item> 
      <el-form-item label="宿舍长号" label-width="120px">
      <el-input v-model="dormitoryHead.sNo" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label-width="200px">
      <el-button size="small"  type="primary" @click="ensureAddDormitoryHead()">添加</el-button>
      <el-button size="small"  type="primary" @click="cancelAddDormitoryHead()">取消</el-button>
    </el-form-item>
  </el-form>
  </el-dialog>

  <!-- 修改学生隐藏端口 -->
    <el-dialog title="修改宿舍长" :visible.sync="updateDialogFormVisible">
  <el-form :model="uDormitoryHead">
    <el-form-item label="姓名" label-width="120px">
      <el-input v-model="uDormitoryHead.username" autocomplete="off"></el-input>
    </el-form-item> 
    <el-form-item label="密码" label-width="120px">
      <el-input type="password" v-model="uDormitoryHead.password" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" label-width="120px">
      <el-input v-model="uDormitoryHead.email" autocomplete="off"></el-input>
    </el-form-item> 
      <el-form-item label="宿舍长号" label-width="120px">
      <el-input v-model="uDormitoryHead.sNo" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label-width="200px">
      <el-button size="small"  type="primary" @click="ensureUpdateDormitoryHead()">修改</el-button>
      <el-button size="small"  type="primary" @click="cancelUpdateDormitoryHead()">取消</el-button>
    </el-form-item>
  </el-form>
  </el-dialog>

  <!-- 页面主体 -->
   <el-row :gutter="0">

     <!-- 添加学生按钮 -->
    <el-col :span="3">
      <el-button size="small"  type="primary" @click="addDormitoryHead()">添加宿舍长</el-button>
    </el-col>

    <!-- 输入框 -->
    <el-col :span="6">
        <el-input
        v-model="dormitoryHeadDTO.username"
        class="w-50 m-2"
        size="small"
        placeholder="请输入姓名"
        @input="search()"
        />
    </el-col>

    <!-- 搜索按钮 -->
    <el-col :span="3">
      <el-button size="small"  type="primary" @click="search()">搜索</el-button>
    </el-col>

     <!-- 导出Excel -->
    <el-col :span="3">
      <el-button size="small"  type="primary" @click="exportExcel()">导出Excel</el-button>
    </el-col>

  </el-row>

  <!-- 表格 -->
  <el-table :data="dormitoryHeads" stripe border height="300" id="mytable">
    <el-table-column prop="id" sortable=true label="ID"/>
    <el-table-column prop="user.username" sortable=true label="昵称"/>
    <el-table-column prop="user.email" sortable=true label="邮箱" />
    <el-table-column prop="sNo" sortable=true label="宿舍长号" />
        <el-table-column label="操作">
            <template #default="scope">
                <el-button size="small" @click="updateDormitoryHead(scope.row)">
                   修改
                </el-button>
                <el-button size="small" type="danger" @click="deleteDormitoryHead(scope.row.id)">
                   删除
                </el-button>
            </template>
            </el-table-column>
  </el-table>

  <!-- 分页部分 -->
  <el-pagination
    small
    background
    layout="prev, pager, next"
    :page-size="dormitoryHeadDTO.capacity"
    :current-page.sync="dormitoryHeadDTO.index"
    @current-change="changeCurrent"
    :total="count"
    class="mt-4"
  />
</template>

</div>
</template>
<script>
export default {
  name: 'Home',
  data () {
    return {
      // 当前登录的用户角色
      role: '学生',
      // 表格映射对象
      dormitoryHeads: [],
      // 修改的对象
      uDormitoryHead: {
        userJson: {
          '@type': 'com.nciae.po.User',
            username: '',
            password: '',
            email: ''
        },
        username: '',
        password: '',
        email: '',
        sNo: ''
      },
      // 表格查询对象
      dormitoryHeadDTO: {
          index: 1,
          capacity: 10,
          username: ''
      },
      // 角色列表
      roles: [],
      // 添加学生弹出控制
      dialogFormVisible: false,
      // 修改学生弹出控制
      updateDialogFormVisible: false,
      // 添加学生实体
      dormitoryHead: {
        userJson: {
          '@type': 'com.nciae.po.User',
            username: '',
            password: '',
            email: '',
        },
        username: '',
        password: '',
        email: '',
        sNo: ''
      },
      // 学生列表总数
      count: 0,
      // 用户信息
      user: {},
      // 角色信息
      role: ''

    }
  },
  // 页面初始化函数 
  created () {

    // 获得角色信息
    this.role = window.sessionStorage.getItem('role')

    // 获得用户信息
    this.user = JSON.parse(window.sessionStorage.getItem('user'))

    this.$http({
        url: "/dormitoryHead",
        params: {
            index: this.dormitoryHeadDTO.index,
            capacity: this.dormitoryHeadDTO.capacity,
            username: this.dormitoryHeadDTO.username
        }
    }).then(response => {
       this.dormitoryHeads = response.data
       console.log(this.dormitoryHeads)
    })
  },
  methods: {


    // 改变当前页
    changeCurrent() {
      findAll()
    },


    // 添加学生
     addDormitoryHead() {
       this.dialogFormVisible=true;
     },

     // 修改时对象映射
     updateDormitoryHead(data) {

       this.uDormitoryHead.id = data.user.id
       this.uDormitoryHead.username=data.user.username
       this.uDormitoryHead.password=data.user.password
       this.uDormitoryHead.email=data.user.email
       this.uDormitoryHead.sNo=data.sNo
       this.updateDialogFormVisible=true

     },

     // 删除宿舍长
     deleteDormitoryHead(id) {
        this.$http.delete('/dormitoryHead/' + id).then(response => {
          this.$message({
          message: '删除宿舍长成功',
          type: 'success'
          });
          this.findAll()
        })
     },

     // 添加宿舍长
     ensureAddDormitoryHead() {
      this.dormitoryHead.userJson.username=this.dormitoryHead.username
      this.dormitoryHead.userJson.password=this.dormitoryHead.password
      this.dormitoryHead.userJson.email=this.dormitoryHead.email
      this.dormitoryHead.userJson = JSON.stringify(this.dormitoryHead.userJson)
      this.$http.post("/dormitoryHead", this.dormitoryHead).then(response => {
        this.$message({
          message: '添加宿舍长成功',
          type: 'success'
        });
        this.clearDormitoryHead()
        this.findAll()
      })
    },

    // 取消添加宿舍长
    cancelAddDormitoryHead() {
      this.clearDormitoryHead()
    },

    // 修改宿舍长
    ensureUpdateDormitoryHead() {

      this.uDormitoryHead.userJson.username=this.uDormitoryHead.id
      this.uDormitoryHead.userJson.username=this.uDormitoryHead.username
      this.uDormitoryHead.userJson.password=this.uDormitoryHead.password
      this.uDormitoryHead.userJson.email=this.uDormitoryHead.email
      this.uDormitoryHead.userJson = JSON.stringify(this.uDormitoryHead.userJson)

      this.$http.put('/dormitoryHead', this.uDormitoryHead).then(response => {
        this.$message({
          message: '修改宿舍长成功',
          type: 'success'
        });   
        this.updateDialogFormVisible=false
        this.userJson = {'@type': 'com.nciae.po.User', username: '', password:'', email: '', sNo: ''}
        this.findAll()
      })
    },

    // 取消修改
    cancelUpdateDormitoryHead() {
      this.updateDialogFormVisible=false
    },


    // 搜索对象，并映射
    search() {
      this.findAll()
    },


    // 查询所有，并映射
    findAll() {
    this.$http({
        url: "/dormitoryHead",
        params: {
            index: this.dormitoryHeadDTO.index,
            capacity: this.dormitoryHeadDTO.capacity,
            username: this.dormitoryHeadDTO.username
        }
    }).then(response => {
       this.dormitoryHeads = response.data
       console.log(this.dormitoryHeads)
    })
    },

    // 清除学生，并关闭窗口
    clearDormitoryHead() {
        this.dormitoryHead.username=''
        this.dormitoryHead.password=''
        this.dormitoryHead.email=''
        this.dormitoryHead.sNo=''
        this.dormitoryHead.userJson={'@type': 'com.nciae.po.User'}
        this.dialogFormVisible=false
     },
    // 导出Excel
    exportExcel () {
        var xlsxParam = { raw: true } 
        var wb = this.$xlsx.utils.table_to_book(document.querySelector('#mytable'), xlsxParam)
        /* get binary string as output */
        var wbout = this.$xlsx.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' })
        try {
            this.$filesaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), this.user.username + '导出宿舍长信息表.xlsx')
        } catch (e) {
        if (typeof console !== 'undefined') {
            console.log(e, wbout)
        }
        }
        return wbout
    }

  }
}
</script>
<style>

</style>
